package me.lc.superDown.scene;

import me.lc.superDown.core.GameManager;
import me.lc.superDown.core.GameMap;
import me.lc.superDown.core.GameSystem;
import me.lc.superDown.layer.FinishLayer;
import me.lc.superDown.layer.GameBgLayer;
import me.lc.superDown.layer.GameLayer;
import me.lc.superDown.layer.MainLayer;
import me.lc.superDown.layer.PauseLayer;
import android.view.KeyEvent;

import com.wiyun.engine.nodes.Scene;

public class GameScene extends Scene {
	private static GameScene instance = null;
	private static Object sync_obj = new Object();

	public GameLayer layer;
	public GameBgLayer bgLayer;
	public MainLayer mainLayer;
	public FinishLayer finishLayer;
	public PauseLayer pauseLayer;

	private GameScene() {
		bgLayer = new GameBgLayer();
		addChild(bgLayer, 1);

		mainLayer = new MainLayer();
		addChild(mainLayer, 3);

		layer = new GameLayer();
		addChild(layer, 2);

		finishLayer = new FinishLayer();
		finishLayer.setVisible(false);
		addChild(finishLayer, 4);

		pauseLayer = new PauseLayer();
		pauseLayer.setVisible(false);
		addChild(pauseLayer, 5);
	}

	public static GameScene make() {
		synchronized (sync_obj) {
			if (instance == null) {
				instance = new GameScene();
			}
			return instance;
		}
	}

	@Override
	public boolean wyKeyDown(KeyEvent event) {

		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			if (GameManager.instance.isStarted
					&& !GameManager.instance.isPaused) {
				GameManager.instance.isPaused = true;
				pauseLayer.setVisible(true);
				layer.btn_pause.setVisible(false);
				GameMap.instance.standGuy();
			} else if (!GameManager.instance.isStarted
					&& !GameManager.instance.isFinished) {
				GameSystem.AndroidContext.showExitTips();
			}
			return true;
		}
		return super.wyKeyDown(event);
	}
}
